package io.renren.controller;

import io.renren.entity.FileDestructionEntity;
import io.renren.entity.SysUserEntity;
import io.renren.service.FileDestructionService;
import io.renren.utils.PageUtils;
import io.renren.utils.QueryAuthority;
import io.renren.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 档案销毁
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-29 15:58:30
 */
@RestController
@RequestMapping("filedestruction")
public class FileDestructionController  extends  AbstractController{
	@Autowired
	private FileDestructionService fileDestructionService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("filedestruction:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        QueryAuthority query = new QueryAuthority(params);

		List<FileDestructionEntity> fileDestructionList = fileDestructionService.queryList(query);
		int total = fileDestructionService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(fileDestructionList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("filedestruction:info")
	public R info(@PathVariable("id") Integer id){
		FileDestructionEntity fileDestruction = fileDestructionService.queryObject(id);
		
		return R.ok().put("fileDestruction", fileDestruction);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("filedestruction:save")
	public R save(@RequestBody FileDestructionEntity fileDestruction){
		SysUserEntity entity=getUser();
		fileDestruction.setCdate(new Date());
		fileDestruction.setCusername(entity.getUsername());
		fileDestruction.setCompany(entity.getCompany());
		fileDestruction.setDept(entity.getDept());
		fileDestruction.setCompanyId(entity.getCompanyId());
		fileDestruction.setUserdesc(entity.getUserdesc());

		fileDestruction.setDeptId(entity.getDeptNo());
		fileDestruction.setPost(entity.getPost());
		fileDestructionService.save(fileDestruction);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("filedestruction:update")
	public R update(@RequestBody FileDestructionEntity fileDestruction){
		fileDestructionService.update(fileDestruction);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("filedestruction:delete")
	public R delete(@RequestBody Integer[] ids){
		fileDestructionService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 提交
	 */
	@RequestMapping("/submit")
	@RequiresPermissions("filedestruction:submit")
	public R submit(@RequestBody Integer id){
		FileDestructionEntity filedestruction = fileDestructionService.queryObject(id);
		String msg=fileDestructionService.submit(filedestruction);
		if(StringUtils.isNotBlank(msg)){
			return R.error(1, msg);
		}
		return R.ok();
	}


	/**
	 * 批准
	 */
	@RequestMapping("/process")
	public R process(@RequestBody FileDestructionEntity filedestruction){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(filedestruction.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");
		}

		fileDestructionService.process(filedestruction);

		return R.ok();
	}

	/**
	 * 拒绝
	 */
	@RequestMapping("/reject")
	public R reject(@RequestBody FileDestructionEntity filedestruction){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(filedestruction.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");

		}
		fileDestructionService.reject(filedestruction);

		return R.ok();
	}
	
}
